package com.alifi.crm.biz.guess;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.common.lang.StringUtil;
import com.alifi.crm.dal.enu.States;
import com.alifi.crm.dal.enu.YesOrNo;
import com.alifi.crm.dal.object.EmployeeGuessDO;

/**
 * 
 * @author wenjing.huangwj
 *
 */
public class Base {

	
	public final static Long maxLimitTimes = 5L;
	
	public final static String win = "WIN";
	
	private final static Long endDateStrLong = 20130320L;
	
	private static Random random = new Random(System.currentTimeMillis());
	
	private static int compareValue = 1;
	
	
	public static boolean isHit(int chance){
		int value = random.nextInt(chance);
		if(compareValue==value){
			return true;
		}
		return false;
	}
	
	/**
	 * 检查是否不能再猜并设置文案
	 * @param guessDO
	 * @param ctx
	 */
	public static boolean checkAndSetError(EmployeeGuessDO guessDO,final Context ctx,EmployeeGuessManager employeeGuessManager){	
		if(States.结束.equals(guessDO.getStates())){
			if(YesOrNo.正确.equals(guessDO.getIsRight())){
				ctx.put("error", GuessMessage.GUESS_RIGHT_REPEAT);
			}
			else{
				ctx.put("error", GuessMessage.GUESS_WRONG_OUT_LIMIT_END);
			}
			return true;
		}
		else if(guessDO.getLimitGuessTimes()<=guessDO.getGuessTimes()){  //大于等于上限次数
			if(maxLimitTimes<=guessDO.getGuessTimes()){     //且大于等于3次
				if(Base.isSmallerEndDate()){
					ctx.put("error", GuessMessage.GUESS_WRONG_OUT_LIMIT_TOMORROW_CONTINUE);
				}else{
					ctx.put("error", GuessMessage.GUESS_WRONG_OUT_LIMIT_END);
				}
			}
			else{   //小于 3次
				ctx.put("error", GuessMessage.GUESS_WRONG_OUT_LIMIT_CONTINUE);
			}
			return true;
		}
		else if(employeeGuessManager.getRedEnvelopeCountDO(new SimpleDateFormat("yyyyMMdd").format(new Date()))==null){
			if(isSmallerEndDate()){
				ctx.put("error", GuessMessage.GAME_NOT_START);
			}else{
				ctx.put("error", GuessMessage.GAME_HAS_EXPIRE);
			}
			return true;
		}
		return false;
	}
	
	
	/**
	 * 减法
	 * @param subtrahend  减数
	 * @param minuend     被减数
	 * @return
	 */
	public static String toStringSubtract(Long subtrahend,Long minuend){
		if(subtrahend==null||minuend==null){
			return "0";
		}
		long result = subtrahend.longValue() - minuend.longValue();
		if(result<0){
			return "0";
		}
		return String.valueOf(result);
	}
	
	/**
	 * 如果当前时间小于endDateStrLong,返回true,否则返回false
	 * @return
	 */
	public static boolean isSmallerEndDate(){
		String currentDateStr = new SimpleDateFormat("yyyyMMdd").format(new Date());
		Long currentDateStrLong = Long.parseLong(currentDateStr);
		if(currentDateStrLong<Base.endDateStrLong){
			return true;
		}
		return false;
	}
	
	/**
	 * 变为六位工号,不足六位前面补0
	 * @param employeeNum
	 * @return
	 */
	public static String toSixBitSmployee(String employeeNum){
		if(StringUtil.isBlank(employeeNum)){
			return employeeNum;
		}
		String employeeNumTrim = StringUtil.trim(employeeNum);
		if(employeeNumTrim.length()==1){
			return "00000"+employeeNumTrim;
		}
		else if(employeeNumTrim.length()==2){
			return "0000"+employeeNumTrim;
		}
		else if(employeeNumTrim.length()==3){
			return "000"+employeeNumTrim;
		}
		else if(employeeNumTrim.length()==4){
			return "00"+employeeNumTrim;
		}
		else if(employeeNumTrim.length()==5){
			return "0"+employeeNumTrim;
		}
		return employeeNumTrim;
	}
	
	
}
